import yaml
import json
from conftest import BASE_DIR

def read_yaml(filename="login.yaml"):
    file_path = BASE_DIR  + "/data/" +  filename
    # 定义空列表 组装测试数据
    arr = []
    # 获取文件流
    with open(file_path, "r", encoding="utf-8") as f:
        # 遍历 调用yaml.safe_load(f).values()方法
        #yaml.safe_load(f)格式为{"login1":{"key1":"value1"，..},"login2":{"key1":"value1",..},..}
        for data in yaml.safe_load(f).values():
            # 使用tuple(json字符串)转成元组
            arr.append(tuple(data.values()))
    #最终返回arr的格式为[(),()，..]的元组列表
    return arr

def read_json(filename="login.json"):
    file_path = BASE_DIR + "/data/" + filename
    # 定义空列表 组装测试数据
    arr= []
    with open(file_path, "r", encoding="utf-8") as f:
        #json.load(f)返回的结果格式为[{"key1":"value1"，..},{"key1":"value1",..},..]
        for data in json.load(f):
            #使用tuple(json字符串)转成元组
            arr.append(tuple(data.values()))
    #最终返回arr的格式为[(),()，..]的元组列表
    return arr

#调试
# if __name__ == '__main__':
    #json格式转为元组，使用tuple(json字符串)转成元组
    # json_data={'case_name': '登陆成功', 'mobile': '13800000002', 'password': '123456'}
    # print("将json格式转为元组:",tuple(json_data))
    # print(read_yaml("login.yaml"))
    # print(read_json("login.json"))

